package com.jci.vsd.activity.Reim;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.jci.vsd.R;
import com.jci.vsd.activity.BaseActivity;
import com.jci.vsd.adapter.TimeLineAdapter;
import com.jci.vsd.adapter.reim.ApprovalDetailRecycleAdapter;
import com.jci.vsd.application.VsdApplication;
import com.jci.vsd.bean.download.FileCallBack;
import com.jci.vsd.bean.download.FileSubscriber;
import com.jci.vsd.bean.reim.ApprovalAllDetailBean;
import com.jci.vsd.bean.reim.MyReimDetailBean;
import com.jci.vsd.bean.reim.ReimDocSubmitBean;
import com.jci.vsd.bean.reim.ReimPicBean;
import com.jci.vsd.bean.reim.SubmitApprovalBean;
import com.jci.vsd.bean.reim.WaitApprovalDetailBean;
import com.jci.vsd.constant.MySpEdit;
import com.jci.vsd.network.control.DownloadFileControl;
import com.jci.vsd.network.control.ReimControl;
import com.jci.vsd.observer.CommonDialogObserver;
import com.jci.vsd.observer.RxHelper;
import com.jci.vsd.utils.BitmapUtil;
import com.jci.vsd.utils.FileUtils;
import com.jci.vsd.utils.Loger;
import com.jci.vsd.utils.StrTobaseUtil;
import com.jci.vsd.utils.Utils;
import com.jci.vsd.view.widget.SimpleToast;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

import cn.unitid.spark.cm.sdk.business.service.SignatureP1Service;
import cn.unitid.spark.cm.sdk.config.PkiDataType;
import cn.unitid.spark.cm.sdk.data.response.DataProcessResponse;
import cn.unitid.spark.cm.sdk.exception.CmSdkException;
import cn.unitid.spark.cm.sdk.listener.ProcessListener;
import io.reactivex.Observable;
import me.iwf.photopicker.PhotoPreview;
import okhttp3.ResponseBody;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

import static com.jci.vsd.constant.AppConstant.fileStoreDir;

/**
 * Created by liqing on 18/6/28.
 * 我的审批 -审批详情
 */

public class MyApprovalProcessActivity extends BaseActivity {
    @BindView(R.id.rl_approval_detail)
    RecyclerView rlApprovalDetail;
    @BindView(R.id.rl_time_line)
    RecyclerView rlTimeLine;
    @BindView(R.id.iv_reim_pic)
    ImageView ivReimPic;

    @BindView(R.id.tv_approval_unpass)
    TextView tvUnpass;
    @BindView(R.id.tv_approval_pass)
    TextView tvPass;
    //    @BindView(R.id.button_back);
    @BindView(R.id.button_back)
    ImageButton backBtn;

    @BindView(R.id.textview_title)
    TextView titleTxt;


    private Context context;
    private ApprovalDetailRecycleAdapter adapter;
    private LinearLayoutManager layoutReimManager;


    private List<MyReimDetailBean.CostsBean> approvalReimbeanList;
    private String reason = "";
    //    private boolean approveResultId;
//    private int level = 0;
    private String picPath;
    private String cert = "";
    private String sign = "";
    private int id;
    private String approvalResult;
    private List<String> selectPic;
    Subscription subscription;
    private String hashFileReim;
    private List<String> itemPic;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wait_approval_detail);
        context = MyApprovalProcessActivity.this;
        titleTxt.setText(getResources().getString(R.string.my_approval_detail));
        itemPic = new ArrayList<>();
        initViewEvent();
        layoutReimManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        // layoutTimeManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        approvalReimbeanList = new ArrayList<>();
        initRecyApproval();
        id = Integer.valueOf(getIntent().getStringExtra("id"));
        Loger.e("---approval id" + id);
        getData(id);

    }


    private void initRecyApproval() {
        rlApprovalDetail.setLayoutManager(layoutReimManager);
        adapter = new ApprovalDetailRecycleAdapter(context, approvalReimbeanList);
        rlApprovalDetail.setAdapter(adapter);
        adapter.setOnItemClickListener(new ApprovalDetailRecycleAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int pos) {
                // toAtivityWithParams(WaitApprovalPicActivity.class, approvalReimbeanList.get(pos));
            }

            @Override
            public void onPicCLick(View view, int pos) {

                Loger.e("--picclick" + approvalReimbeanList.get(pos).getPath());
                toActivityWithParams(WaitApprovalPicActivity.class,"url", approvalReimbeanList.get(pos).getUrl());

            }
        });
    }


    @Override
    protected void initViewEvent() {
        ivReimPic.setOnClickListener(this);
        tvPass.setOnClickListener(this);
        tvUnpass.setOnClickListener(this);
        backBtn.setOnClickListener(this);

   // backbtn
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.iv_reim_pic:
                //查看大图的报销单据照片
                selectPic = new ArrayList<>();
                selectPic.add(picPath);
                PhotoPreview.builder()
                        .setPhotos((ArrayList) selectPic)
                        .setShowDeleteButton(false)
                        .start(MyApprovalProcessActivity.this, PhotoPreview.REQUEST_CODE);
                break;
            case R.id.tv_approval_pass:
                //通过，先去签名数据
                approvalResult = "true";
                try {
                    if (!TextUtils.isEmpty(hashFileReim)) {
                        //跳过
                       // signVerifyP1(hashFileReim);

                        SubmitApprovalBean bean = new SubmitApprovalBean();
                        bean.setCer(cert);
                        bean.setSign(sign);
                        bean.setFlag(approvalResult);
//                    id = 48;
                        Loger.e("--pass Id" + id);
                        bean.setId(id);
                        bean.setReason(reason);
                        submitApproval(bean);
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }
                Loger.e("--hashFileReim--" + hashFileReim);
  // analysize 返回 超时问题 重连
                break;
            case R.id.tv_approval_unpass:
                //填写理由
                showReasonDialog();
                break;
            default:
                break;
        }
    }


    private void showReasonDialog() {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("请填写不通过理由");
        final EditText input = new EditText(this);
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
        input.setInputType(InputType.TYPE_CLASS_TEXT);
        builder.setView(input);

// Set up the buttons
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {


            @Override
            public void onClick(DialogInterface dialog, int which) {
                reason = input.getText().toString();
                //pass=1 ;unpass =2
                approvalResult = "false";
                if (TextUtils.isEmpty(reason)) {
                    SimpleToast.toastMessage("请填写不通过理由", Toast.LENGTH_SHORT);
                    return;
                }

                try {
                    if (!TextUtils.isEmpty(hashFileReim)) {
                        //跳过
                      //  signVerifyP1(hashFileReim);

                        SubmitApprovalBean bean = new SubmitApprovalBean();
                        bean.setCer(cert);
                        bean.setSign(sign);
                        bean.setFlag(approvalResult);
                        Loger.e("--pass Id" + id);
                        bean.setId(id);
                        bean.setReason(reason);
                        submitApproval(bean);
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }
                Loger.e("--hashFileReim--" + hashFileReim);


            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        builder.show();
    }

    private void signVerifyP1(final String base64Code1) {
        String userId = VsdApplication.getUserId();
        final SignatureP1Service signatureP1Service = new SignatureP1Service(MyApprovalProcessActivity.this,
                userId, new
                ProcessListener<DataProcessResponse>() {
                    @Override
                    public void doFinish(DataProcessResponse dataProcessResponse, String certificate) {
//                        hideLoading();
                        if (dataProcessResponse.getRet() == 0) {
                            //  cipherText.setText(dataProcessResponse.getResult());
                            // cert.setText(certificate);
                            cert = certificate;
                            sign = dataProcessResponse.getResult();
                            // spu.setCert(certificate);
                            SubmitApprovalBean bean = new SubmitApprovalBean();
                            bean.setCer(cert);
                            bean.setSign(sign);
                            bean.setFlag(approvalResult);
//                    id = 48;
                            Loger.e("--pass Id" + id);
                            bean.setId(id);
                            bean.setReason(reason);
                            submitApproval(bean);

                        } else {
                            Toast.makeText(MyApprovalProcessActivity.this, "报销单签名失败,请重试!" + dataProcessResponse.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void doCancel() {
                        // hideLoading();
                    }

                    @Override
                    public void doException(CmSdkException e) {
                        //  hideLoading();
                        Toast.makeText(MyApprovalProcessActivity.this, "报销单签名失败,请重试!" + e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
        signatureP1Service.p1Signature(base64Code1, PkiDataType.DATA_STRING_TYPE,
                null);
    }


    private void getData(int id) {
        Observable<MyReimDetailBean> observable = new ReimControl().getMyReimDetail(id);
        CommonDialogObserver<MyReimDetailBean> observer = new CommonDialogObserver<MyReimDetailBean>(this) {
            @Override
            public void onNext(MyReimDetailBean bean) {
                super.onNext(bean);

                if (bean != null) {

                    if (bean.getCosts().size() == 0) {
                        SimpleToast.toastMessage("暂无审批", Toast.LENGTH_SHORT);

                    } else {
                       // SimpleToast.toastMessage("获取成功", Toast.LENGTH_SHORT);
                        String picUrl = bean.getUrl();
                        String fileName = System.currentTimeMillis() + ".png";
                        downLoadPic(fileName, picUrl, "1");
                        approvalReimbeanList.clear();
                        approvalReimbeanList.addAll(bean.getCosts());
                        adapter.notifyDataSetChanged();

                        for (int i = 0; i < approvalReimbeanList.size(); i++) {
                            String url = approvalReimbeanList.get(i).getUrl();
                            String itemPathName = searchlastString(url, "/");
                            approvalReimbeanList.get(i).setPath(itemPathName);

                        }


                    }

                }
            }

            @Override
            public void onError(Throwable t) {
                super.onError(t);
            }
        };
        RxHelper.bindOnUIActivityLifeCycle(observable, observer, MyApprovalProcessActivity.this);


    }

    //  截取名字

    public String searchlastString(String string, String signal) {

        String substring = "";
        int lastIndex = string.lastIndexOf(signal);

        if (lastIndex == -1) {
            System.out.println("没有找到字符串/and.");
        } else {
            System.out.println("Hello 字符串最后出现的位置： " + lastIndex);
            substring = string.substring(lastIndex);
        }
        return substring;
    }


    //获取报销单图片
    public void downLoadPic(final String fileName, String url, final String type) {
        //type ="1": 报销单图片； type＝"2"：报销项图片
        final FileCallBack<ResponseBody> callBack = new FileCallBack<ResponseBody>(fileStoreDir, fileName) {

            @Override
            public void onSuccess(final ResponseBody responseBody) {
                String path = fileStoreDir + "/" + fileName;

                if (type.equals("1")) {
                    picPath = path;
                    Glide.with(MyApprovalProcessActivity.this)
                            .load(path)
                            .into(ivReimPic);

                    try {
                        hashFileReim = FileUtils.getMD5Checksum(path);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    Loger.e("--hashFileReim" + hashFileReim);
                } else if (type.equals("2")) {

                    itemPic.clear();
                    itemPic.add(0, path);
                    PhotoPreview.builder()
                            .setPhotos((ArrayList) itemPic)
                            .setShowDeleteButton(false)
                            .start(MyApprovalProcessActivity.this, PhotoPreview.REQUEST_CODE);
                }
                Loger.e("--downLoad--success +path=" + path);


            }

            @Override
            public void progress(long progress, long total) {
                int progressIndex = (int) (progress * 100 / total);
                Loger.i("progress = " + progress + "," + total + "," + Thread.currentThread().getId() + "," + progressIndex);
//                updateProgressBar.setProgress(progressIndex);
            }

            @Override
            public void onStart() {
                showProgress();
            }

            @Override
            public void onCompleted() {
                //updateProgressBar.setVisibility(View.INVISIBLE);
                dimissProgress();
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                Loger.e("FileCallback--onError" + e.getMessage());
                dimissProgress();
                SimpleToast.toastMessage(e.getMessage(), Toast.LENGTH_SHORT);
//                updateProgressBar.setVisibility(View.INVISIBLE);
//                sureBtn.setEnabled(true);
//                cancelBtn.setEnabled(true);
            }
        };
        // rx.Observable<ResponseBody> obserable = new DownloadFileControl().downloadReimFile(url);
        rx.Observable<ResponseBody> obserable = new DownloadFileControl().downloadReimFileWithToken(url);
        subscription = obserable.subscribeOn(Schedulers.io())//请求网络 在调度者的io线程
                .observeOn(Schedulers.io()) //指定线程保存文件
                .doOnNext(new Action1<ResponseBody>() {
                    @Override
                    public void call(ResponseBody body) {
                        try {
                            callBack.saveFile(body);
                        } catch (Exception e) {
                            e.printStackTrace();
                            callBack.onError(e);
                            Utils.doException(e);
                        }
                    }
                }).observeOn(AndroidSchedulers.mainThread()) //指定线程保存文件
                .doOnError(new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        Utils.doException(throwable);
                    }
                }).observeOn(AndroidSchedulers.mainThread()) //在主线程中更新ui
                .subscribe(new FileSubscriber<ResponseBody>(VsdApplication.getInstance(), callBack));//在主线程中更新ui
        //new DownloadAppControl().downloadApp(getActivity(),url,callBack);
    }


    private void submitApproval(SubmitApprovalBean bean) {

        Observable<Boolean> observable = new ReimControl().submitApproval(bean);
        CommonDialogObserver<Boolean> observer = new CommonDialogObserver<Boolean>(this) {
            @Override
            public void onNext(Boolean boo) {
                super.onNext(boo);
                SimpleToast.toastMessage("提交成功。", Toast.LENGTH_SHORT);
//                toActivity(MyApprovalRecyActivity.class);
                finish();

            }

            @Override
            public void onError(Throwable t) {
                super.onError(t);
            }
        };

        RxHelper.bindOnUIActivityLifeCycle(observable, observer, MyApprovalProcessActivity.this);
    }

}
